package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.BlockChainTypeDao;
import com.sc.nft.dao.UserAssetsDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.*;
import com.sc.nft.enums.AssetChangeLogEnum;
import com.sc.nft.enums.BlockChainTypeEnum;
import com.sc.nft.enums.IntegralTaskEnum;
import com.sc.nft.service.UserChainAddrService;
import com.sc.nft.service.UserReceiveIntegralService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.BlockChainTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.web3j.crypto.Hash;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 服务接口实现
 *
 * @author kancy
 * @since 2022-06-01 15:02:01
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BlockChainTypeServiceImpl implements BlockChainTypeService {

    private final UserReceiveIntegralService userReceiveIntegralService;
    private final UserAssetsDao userAssetsDao;
    private final BlockChainTypeDao blockChainTypeDao;
    private final UserInfoDao userInfoDao;
    private final UserChainAddrService userChainAddrService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUserAssets(Long userId) {

        BlockChainType blockChainType = blockChainTypeDao.getById(BlockChainTypeEnum.INTEGRAL.getCode());

        UserAssets model = userAssetsDao.getOneByUserIdAndBlockChainId(userId,blockChainType.getId());
        if(ObjectUtil.isEmpty(model)){
            UserAssets userAssets=new UserAssets();
            userAssets.setBlockChainId(blockChainType.getId());
            userAssets.setUserId(userId);
            userAssets.setCreateTime(new Date());
            userAssets.insert();

            //用户注册成功后发放积分奖励
            userReceiveIntegralService.completeOtherIntegralTask(userId, IntegralTaskEnum.USER_REGIST, AssetChangeLogEnum.USER_REGIST);
            userInfoDao.updateIsAddrById(userId,Boolean.TRUE);
        }
    }

    @Override
    public void insertUserChainMaker(Long userId) {
        BlockChainType blockChainType = blockChainTypeDao.getOneByStatusAndInit(BlockChainTypeEnum.CHAINMAKER.getCode());
        UserChainAddr userChainAddr  =userChainAddrService.getOneByUserIdAndBlockChainId(userId,blockChainType.getId());
        UserInfo user = userInfoDao.getById(userId);
        if(ObjectUtil.isEmpty(userChainAddr) && ObjectUtil.isNotEmpty(user)){
            UserChainAddr chainAddr = new UserChainAddr();
            chainAddr.setUserId(userId);
            chainAddr.setBlockChainId(blockChainType.getId());
            chainAddr.setBlockChainName(blockChainType.getChainNameEn());
            chainAddr.setTokenAddress(Hash.sha3(user.getUserUuid()));
//            chainAddr.setTokenAddressKey("");
            chainAddr.insert();
        }
    }

    @Override
    public void intUserWallet(Long id) {
        UserWallet userWallet = new UserWallet();
        userWallet.setUserId(id);
        userWallet.setSumIncome(BigDecimal.ZERO);
        userWallet.setBalance(BigDecimal.ZERO);
        userWallet.setWithdrawingAmount(BigDecimal.ZERO);
        userWallet.setTransferAmount(BigDecimal.ZERO);
        userWallet.setSumWithdraw(BigDecimal.ZERO);
        userWallet.insert();
    }
}